import plotly.io as pio
pio.renderers.default = "notebook"
from dwave.system import DWaveSampler, EmbeddingComposite
from schedule import *
import neal
import plotly.express as px
from tabu import TabuSampler
from dwave_qbsolv import QBSolv
from anneal_solver import solvelog_dwave
#Flag damit nicht unbeabsichtigt der quantumcomputer verwendet wird
use_quantum=True
#maximale Zeit
s = Schedule(time_max=10)
# maximal 3 maschinen
# bitte nur benötigte anzahl der maschinen angeben, wir haben keinen preprocessing step um unnötige maschinen heraus zufiltern
s.build_machines(7)
#Jobs
# 1:(2,3) bedeuted auf der maschine 1 wird die arbeit 2 zeiteinheiten am stück durchgeführt und 3 mal wiederholt
# es sollte wenn möglich nur die dauer auf 1 begrenzt sein, da wir keine zeit hatten die anderen optionen gut zu testen
# frühst möglicher anfangszeitpunkte können zwar gesetzt werden, dies bricht zurzeit noch einige feature und sollte deswegen nicht gemacht werden
s.create_job({0: (1, 1), 2: (1, 1)}, parallel_operations=1)
s.create_job({3: (1, 2), 0: (1, 1)}, parallel_operations=1)
s.create_job({4: (1, 1), 0: (1, 1)}, parallel_operations=1)
s.create_job({5: (1, 1), 1: (1, 1), 2: (1, 1)}, parallel_operations=1)
s.create_job({6: (1, 1), 2: (1, 1)}, parallel_operations=1)
print(s)
---------------------------- Arbeitsplan Job 0 Operationen: Machine(nr=0, name='m0', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Machine(nr=2, name='m2', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Job 1 Operationen: Machine(nr=3, name='m3', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 2 Machine(nr=0, name='m0', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Job 2 Operationen: Machine(nr=4, name='m4', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Machine(nr=0, name='m0', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Job 3 Operationen: Machine(nr=5, name='m5', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Machine(nr=1, name='m1', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Machine(nr=2, name='m2', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Job 4 Operationen: Machine(nr=6, name='m6', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 Machine(nr=2, name='m2', parallel_operations=1, start_time=0) Dauer: 1 Anzahl: 1 ----------------------------
qubo=Qubo(s)
#start term
qubo.penalty_terms[1]=1
# nur eine maschine/job pro job/maschine gleichzeiting
qubo.penalty_terms[2]=1
qubo.penalty_terms[3]=1
# 4 hat keine bedeutung, war ursprünglich für die deadlines vorgesehen
# da wir diese aber ohne strafterme lösen, ist dieses feld leer
# Just in time
qubo.penalty_terms[5]=0
qubo.calculate_qubo()
print(f"qbits: {len(qubo.h)}")
# qubo berechnung falls mehrere Maschinen oder Jobs gleichzeitig verwendet werden sollten - (dies muss im schedule angegeben werden)
# qubo.calculate_qubo_virtuell()
qbits: 374
qubo.plot_qubo()
qubo.plot_connections_qubo_states()
qubo.plot_qubo_terms()
response = QBSolv().sample_qubo(qubo.J, num_repeats=1000)
sol3 = response.samples()[0]
*temp, _ = qubo.interpret_solution_dict(sol3)
print(temp)
qubo.plot_solution()
[[1, 24, 78, 107, 116, 152, 199, 243, 248, 284, 321, 365], [state(j:0,m:0,t:1), state(j:0,m:2,t:3), state(j:1,m:0,t:3), state(j:1,m:3,t:0), state(j:1,m:3,t:9), state(j:2,m:0,t:2), state(j:2,m:4,t:6), state(j:3,m:1,t:7), state(j:3,m:2,t:2), state(j:3,m:5,t:6), state(j:4,m:2,t:0), state(j:4,m:6,t:1)], -14.0, {1: -14, 2: 0, 3: 0, 4: 0, 5: 0.0}, 14]
samples = TabuSampler().sample_qubo(qubo.J, num_reads=1000)
sol2 = samples.samples()[0]
*temp, _ = qubo.interpret_solution_dict(sol2)
print(temp)
qubo.plot_solution()
[[0, 22, 76, 107, 109, 152, 193, 236, 248, 279, 321, 370], [state(j:0,m:0,t:0), state(j:0,m:2,t:1), state(j:1,m:0,t:1), state(j:1,m:3,t:0), state(j:1,m:3,t:2), state(j:2,m:0,t:2), state(j:2,m:4,t:0), state(j:3,m:1,t:0), state(j:3,m:2,t:2), state(j:3,m:5,t:1), state(j:4,m:2,t:0), state(j:4,m:6,t:6)], -14.0, {1: -14, 2: 0, 3: 0, 4: 0, 5: 0.0}, 14]
sampler = neal.SimulatedAnnealingSampler()
sampleset = sampler.sample_qubo(qubo.J, num_reads=100)
sol1=sampleset.samples()[0]
*temp,_=qubo.interpret_solution_dict(sol1)
print(temp)
qubo.plot_solution()
[[5, 23, 76, 107, 115, 156, 193, 241, 255, 282, 326, 373], [state(j:0,m:0,t:5), state(j:0,m:2,t:2), state(j:1,m:0,t:1), state(j:1,m:3,t:0), state(j:1,m:3,t:8), state(j:2,m:0,t:6), state(j:2,m:4,t:0), state(j:3,m:1,t:5), state(j:3,m:2,t:9), state(j:3,m:5,t:4), state(j:4,m:2,t:5), state(j:4,m:6,t:9)], -14.0, {1: -14, 2: 0, 3: 0, 4: 0, 5: 0.0}, 14]
if use_quantum:
answer = solvelog_dwave(qubo,samples=10000)
*temp2, _ = qubo.interpret_solution_dict(
{x: y for x, y in answer.samples()[0].items()})
print(temp2)
qubo.plot_solution()
[[31, 22, 12, 13, 228, 57, 47, 69, 82, 51, 308, 325, 101, 251, 327, 108, 111, 147, 334, 262, 127, 197, 222, 343, 239, 285, 286, 353, 369], [state(j:0,m:3,t:0), state(j:0,m:2,t:1), state(j:0,m:1,t:2), state(j:0,m:1,t:3), state(j:3,m:0,t:3), state(j:0,m:5,t:4), state(j:0,m:4,t:5), state(j:0,m:6,t:5), state(j:1,m:0,t:7), state(j:0,m:4,t:9), state(j:4,m:0,t:9), state(j:4,m:2,t:4), state(j:1,m:2,t:5), state(j:3,m:2,t:5), state(j:4,m:2,t:6), state(j:1,m:3,t:1), state(j:1,m:3,t:4), state(j:1,m:6,t:8), state(j:4,m:3,t:3), state(j:3,m:3,t:6), state(j:1,m:4,t:10), state(j:2,m:4,t:4), state(j:2,m:6,t:8), state(j:4,m:4,t:1), state(j:3,m:1,t:3), state(j:3,m:5,t:7), state(j:3,m:5,t:8), state(j:4,m:5,t:0), state(j:4,m:6,t:5)], 11.0, {1: 11, 2: 0, 3: 0, 4: 0, 5: 0.0}, 14]
qubo = Qubo(s)
#start term
qubo.penalty_terms[1] = 5
# nur eine maschine/job pro job/maschine gleichzeiting
qubo.penalty_terms[2] = 1
qubo.penalty_terms[3] = 1
# 4 hat keine bedeutung, war ursprünglich für die deadlines vorgesehen
# da wir diese aber ohne strafterme lösen, ist dieses feld leer
# Just in time
qubo.penalty_terms[5] = 0
qubo.calculate_qubo()
if use_quantum:
answer2 = solvelog_dwave(qubo, samples=10000)
*temp2, _ = qubo.interpret_solution_dict(
{x: y for x, y in answer2.samples()[0].items()})
print(temp2)
qubo.plot_solution()
[[31, 22, 12, 13, 228, 57, 47, 69, 82, 51, 308, 325, 101, 251, 327, 108, 111, 147, 334, 262, 127, 197, 222, 343, 239, 285, 286, 353, 369], [state(j:0,m:3,t:0), state(j:0,m:2,t:1), state(j:0,m:1,t:2), state(j:0,m:1,t:3), state(j:3,m:0,t:3), state(j:0,m:5,t:4), state(j:0,m:4,t:5), state(j:0,m:6,t:5), state(j:1,m:0,t:7), state(j:0,m:4,t:9), state(j:4,m:0,t:9), state(j:4,m:2,t:4), state(j:1,m:2,t:5), state(j:3,m:2,t:5), state(j:4,m:2,t:6), state(j:1,m:3,t:1), state(j:1,m:3,t:4), state(j:1,m:6,t:8), state(j:4,m:3,t:3), state(j:3,m:3,t:6), state(j:1,m:4,t:10), state(j:2,m:4,t:4), state(j:2,m:6,t:8), state(j:4,m:4,t:1), state(j:3,m:1,t:3), state(j:3,m:5,t:7), state(j:3,m:5,t:8), state(j:4,m:5,t:0), state(j:4,m:6,t:5)], 55.0, {1: 55, 2: 0, 3: 0, 4: 0, 5: 0.0}, 14]
*temp2, _ = qubo.interpret_solution_dict(
{x: y for x, y in answer2.samples()[0].items()})
print(temp2)
qubo.plot_solution()
[[0, 54, 300, 24, 14, 79, 155, 27, 40, 159, 97, 324, 252, 329, 128, 108, 110, 92, 116, 257, 333, 189, 127, 194, 195, 271, 213, 237, 285, 286, 358, 366, 369], [state(j:0,m:0,t:0), state(j:0,m:5,t:1), state(j:4,m:0,t:1), state(j:0,m:2,t:3), state(j:0,m:1,t:4), state(j:1,m:0,t:4), state(j:2,m:0,t:5), state(j:0,m:2,t:6), state(j:0,m:3,t:9), state(j:2,m:0,t:9), state(j:1,m:2,t:1), state(j:4,m:2,t:3), state(j:3,m:2,t:6), state(j:4,m:2,t:8), state(j:1,m:5,t:0), state(j:1,m:3,t:1), state(j:1,m:3,t:3), state(j:1,m:1,t:7), state(j:1,m:3,t:9), state(j:3,m:3,t:1), state(j:4,m:3,t:2), state(j:2,m:3,t:7), state(j:1,m:4,t:10), state(j:2,m:4,t:1), state(j:2,m:4,t:2), state(j:3,m:4,t:4), state(j:2,m:5,t:10), state(j:3,m:1,t:1), state(j:3,m:5,t:7), state(j:3,m:5,t:8), state(j:4,m:5,t:5), state(j:4,m:6,t:2), state(j:4,m:6,t:5)], 43.0, {1: 35, 2: 6, 3: 2, 4: 2, 5: 0.0}, 14]
df1=answer.to_pandas_dataframe()
df1
| state(j:0,m:0,t:0) | state(j:0,m:0,t:1) | state(j:0,m:0,t:2) | state(j:0,m:0,t:3) | state(j:0,m:0,t:4) | state(j:0,m:0,t:5) | state(j:0,m:0,t:6) | state(j:0,m:0,t:7) | state(j:0,m:0,t:8) | state(j:0,m:0,t:9) | ... | state(j:4,m:6,t:6) | state(j:4,m:6,t:7) | state(j:4,m:6,t:8) | state(j:4,m:6,t:9) | state(j:4,m:3,t:10) | state(j:4,m:4,t:10) | state(j:4,m:5,t:10) | chain_break_fraction | energy | num_occurrences | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0.053476 | 20.0 | 1 |
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0.064171 | 14.0 | 1 |
| 2 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0.072193 | 13.0 | 1 |
| 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0.040107 | 27.0 | 1 |
| 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0.050802 | 23.0 | 1 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 995 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0.064171 | 62.0 | 1 |
| 996 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0.074866 | 27.0 | 1 |
| 997 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0.080214 | 34.0 | 1 |
| 998 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0.080214 | 45.0 | 1 |
| 999 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0.082888 | 53.0 | 1 |
1000 rows × 377 columns
px.histogram(df1, "energy")
px.histogram(df1, "chain_break_fraction")
df2 = answer2.to_pandas_dataframe()
px.histogram(df2, "energy")
px.histogram(df2, "chain_break_fraction")
# uncomment to export it as html
!jupyter nbconvert --to=html example4.ipynb
[NbConvertApp] WARNING | Config option `kernel_spec_manager_class` not recognized by `NbConvertApp`.
[NbConvertApp] Converting notebook example4.ipynb to html
D:\Anaconda3\envs\Quantum\lib\site-packages\nbconvert\filters\datatypefilter.py:39: UserWarning: Your element with mimetype(s) dict_keys([]) is not able to be represented.
warn("Your element with mimetype(s) {mimetypes}"
[NbConvertApp] Writing 5722091 bytes to example4.html